from collections import OrderedDict

from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


class MyPagination(PageNumberPagination):
    """自定义分页器"""
    page_query_param = 'page'
    page_size_query_param = 'pagesize'

    def get_paginated_response(self, data):
        return Response(OrderedDict([
            ('counts', self.page.paginator.count),  # 获取查询结果总数
            ('lists', data),  # 结果列表
            ('page', self.page.number),  # 当前页码
            ('pages', self.page.paginator.num_pages),  # 总的页码
            ('pagesize',  # 页容量
             int(self.request.query_params.get(
                 self.page_size_query_param))),

        ]))